<?php 
function form_submit($id = 0){ 
	list($dd,$mm,$yyyy) = explode("-" , $_POST['tanggal_lahir']);
	$tanggal = $yyyy .'-'. $mm . '-' . $dd ;
	$lembur = isset($_POST['lembur']) ? 'Ya': 'Tidak';
	 
	$datas = array(
		'idcard'=>my_type_data_str($_POST['tno'].'#'.strtoupper($_POST['idcard'])), 
		'nama'=>my_type_data_str($_POST['nama']), 
		'tempat_lahir'=>my_type_data_int(0), 
		'datetime_added'=>my_type_data_function('NOW()')
	); 
	$jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']);
	if($id == 0){if((int)$jabatan_id == 0){ return false;}}
	list($dda,$mma,$yyyya) = explode("-" , $_POST['tanggal_bekerja']);
	$tanggal_angkat =$yyyya .'-'. $mma . '-' . $dda ;
	 
	$jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']	);
	if($id == 0){ 
		$biodata_id = my_insert_record('biodata' , $datas ); 
		 	
		$karyawan_datas =  array(
			'biodata_id'=>my_type_data_int($biodata_id), 
			'catatan'=>my_type_data_str($_POST['catatan']),
			'nik'=>my_type_data_str($_POST['nik'])
		);
		$karyawan_id = my_insert_record('karyawan' , $karyawan_datas );  
		
		$create_kekaryawanan = array(
					'karyawan_id'=>my_type_data_int($karyawan_id),
					'jabatan_id'=>my_type_data_int($jabatan_id),
					'tmb'=>my_type_data_str($tanggal_angkat),
					'tmb_lm'=>my_type_data_str($tanggal_angkat),
					'gapok_km'=>my_type_data_str((int) $_POST['gapok']),
					'golongan_jabatan_id'=>my_type_data_str((int) $_POST['golongan_jabatan_id']),
					'jabatan_id'=>my_type_data_str((int) $jabatan_id),
					'grade_id'=>my_type_data_str((int) $_POST['grade_id']),
					'jabatan_gaji_id'=>my_type_data_int($jabatan_id)
				);
		my_insert_record('kekaryawanan',$create_kekaryawanan); 
		create_data_karyawan_json($karyawan_id);
		return $karyawan_id;
	}else{  
		$karyawan_datas =  array( 
			'catatan'=>my_type_data_str($_POST['catatan']),
			'nik'=>my_type_data_str($_POST['nik'])
		);
		
		my_update_record('karyawan' , 'karyawan_id' , $id , $karyawan_datas );  
		$create_kekaryawanan = array(
					'gapok_km'=>my_type_data_str((int) $_POST['gapok']),
					'tmb'=>my_type_data_str($tanggal_angkat),
					'tmb_lm'=>my_type_data_str($tanggal_angkat),
					'golongan_jabatan_id'=>my_type_data_str((int) $_POST['golongan_jabatan_id']),
					'jabatan_id'=>my_type_data_str((int) $jabatan_id),
					'grade_id'=>my_type_data_str((int) $_POST['grade_id']),
				);
		my_update_record('kekaryawanan','karyawan_id' , $id , $create_kekaryawanan); 
		create_data_karyawan_json($id); 
		return true;
	}
}

function form_submit_validate(){
	$errsubmit = false;
	$err = array();
	$id = isset($_GET['id']) ? $_GET['id'] : 0;
	if($id == 0){
	/*CHECK CODE*/
	$nik =  trim($_POST["nik"]); 
	$data_code = my_query("SELECT karyawan_id FROM karyawan WHERE nik='{$nik}' "); 
	
	if( $nik == ''){
		//$errsubmit = true;
		$err[] = "Nomor induk pegawai belum di isi";
	}
	elseif(my_num_rows($data_code) > 0){
		//$errsubmit = true;
		$err[] = "Nomor induk pegawai  sudah digunakan";	
	}
	
	if( ! valid_name ($_POST['nama'])   ){
		$errsubmit = true;
		$err[] = "Nama karyawan belum benar";	
	}
	/*
	if( trim($_POST['kelamin']) == ''){
		$errsubmit = true;
		$err[] = "Jenis kelamin belum di pilih";	
	}
	
	list($dd,$mm,$yyy) = explode( "-" , $_POST['tanggal_lahir'] );
	$umur = getage($yyy.'-'.$mm.'-'.$dd);
	if( $umur  < 18 ){
		$errsubmit = true;
		$err[] = "Usia karyawan tidak memenuhi syarat ( {$umur}thn dari minimal 18thn )";	
	}
	*/
	$jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']);
	if( (int) $jabatan_id == 0 ){
		$errsubmit = true;
		$err[] = "Jabatan belum di tentukan";
	}
	
	list($ddk,$mmk,$yyyk) = explode( "-" , $_POST['tanggal_bekerja']);
	if((int) $ddk == 0 OR (int) $mmk == 0 OR (int)$yyyk == 0){
		$errsubmit = true;
		$err[] = "Tanggal mulai bekerja belum benar";
	}
	}
	if((int)$_POST['gapok'] < 1000000){
		$errsubmit = true;
		$err[] = "Gaji pokok belum benar";
	}
	
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}


function form_create($id =0){
my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);
	my_set_file_js(array('components/system/js/calendar/calendarDateInput.js','components/system/jquery/combomulti/jquery.chainedSelects.js')); 
	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

	function selectItem(li) {
		findValue(li);
	}

	function formatItem(row) {
		return   row[0] ;
		}
 
	$(document).ready(function() {
		$("#nama_jabatan").autocomplete(
			"autocomplete_daftar_jabatan.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:5,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
	});

	function checkNameJabatan(name){
		$.get("check_jabatan_names.php", { nama: name  },
		function(data){
			if(data ==  \'0\' ){
				alert(\'Jabatan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\'); 
				$(\'#nama_jabatan\').val(\'\');
				return false;
			} 
		});
	}
	');
	$fields =  my_get_data_by_id('karyawan','karyawan_id',$id); 
	if($fields){
	$kar =  my_get_data_by_id('kekaryawanan','karyawan_id',$id); 
	$jab =  my_get_data_by_id('jabatan','jabatan_id',$kar['jabatan_id']); 
	$bio = my_get_data_by_id('biodata','biodata_id',$fields['biodata_id']);
	}
	$view = form_header( "calon pegawai" , "cp"  );
 //	if($id == 0){
	 $nomor = array(
				'name'=>'nik',
				'value'=>( isset($_POST['nik']) ? $_POST['nik'] : $fields['nik'] ), 
				'id'=>'nik',
				'type'=>'text',
				'size'=>'35', 
			);
	$form_field_no= form_dynamic($nomor);
	
	//$view .= '<tr><td colspan="2"  style="background-color:#F7D3BC;height:24px;">Kekaryawanan</td></tr>';
	$view .= form_field_display( $form_field_no   , "Nomor induk karyawan"    ); 
//	}
	$nama = array(
				'name'=>'nama',
				'value'=>( isset($_POST['nama']) ? $_POST['nama'] : $bio['nama'] ),
				'id'=>'nama',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama); 
	$view .= form_field_display( $form_field_nama   , "Nama"   );
	 
//	$view .= '<tr><td colspan="2" >&nbsp;</td></tr>';
//	$view .= '<tr><td colspan="2"  style="background-color:#F7D3BC;height:24px;">Kekaryawanan</td></tr>';
		$jabatan = array(
			'name'=>'nama_jabatan',
			'value'=>(isset($_POST['nama_jabatan'])? $_POST['nama_jabatan'] : strtoupper( $jab['nama_jabatan'] ) ),
			'id'=>'nama_jabatan',
			'type'=>'textfield',
			'size'=>'60',
			'onchange'=>'javascript:checkNameJabatan(this.value)'
		);
	$form_jabatan = form_dynamic($jabatan);
	$view .= form_field_display( $form_jabatan  , "Jabatan"  );
 
	
	$tanggal_bekerja = array(
				'name'=>'tanggal_bekerja',
				'value'=>( isset($_POST['tanggal_bekerja']) ? $_POST['tanggal_bekerja'] : ( ( $kar['tmb'] <> '') ? date('d-m-Y' , strtotime($kar['tmb']) ) : '01-01-2000') ),
				'id'=>'tanggal_bekerja',
				'type'=>'text','size'=>'55' 
			);
	$form_field_tanggal_bekerja = form_calendar ($tanggal_bekerja);
	$view .= form_field_display( $form_field_tanggal_bekerja   , "Tanggal bekerja"    );
	
	//TAMBAHAN GRADE DAN GOL /****************/
	
	$goljab_id = isset( $kar['golongan_jabatan_id'] ) ? $kar['golongan_jabatan_id']:  0;
	$query = "SELECT * FROM golongan_jabatan ORDER BY kode_golongan_jabatan ASC";
	$result = my_query($query);
	$goljabs = array();
	while( $r = my_fetch_array($result) ){
		$goljabs[$r['golongan_jabatan_id']] =$r['kode_golongan_jabatan'];
	}	
	$golongan_jabatan_id = array(
		'name'=>'golongan_jabatan_id',
		'value'=>( isset($_POST['golongan_jabatan_id'] ) ? $_POST['golongan_jabatan_id'] : $goljab_id  ),
		'id'=>'golongan_jabatan_id' 
		//'onchange'=>'javascript:checkNipar();'		
	);
	$form_temp = form_dropdown($golongan_jabatan_id , $goljabs );
	$view .= form_field_display( $form_temp , "Golongan Jabatan"    );

	$grd_id = isset( $kar['grade_id'] ) ? $kar['grade_id']:  0;
	$grade_id = array(
		'name'=>'grade_id',
		'value'=>( isset($_POST['grade_id'] ) ? $_POST['grade_id'] : $grd_id  ),
		'id'=>'grade_id'  
	);

	if($kar){
		$query_grade = "SELECT grade_id FROM gol_grade 
			WHERE golongan_jabatan_id=".$goljab_id."
GROUP BY grade_id
			ORDER BY grade_id ASC";
		$result_grade = my_query($query_grade);
		$opsi_pilihan ='';
		while($row_grade = my_fetch_array($result_grade)){
			if($row_grade['grade_id'] == $grd_id){
				$opsi_pilihan .='<option selected value="'.$row_grade['grade_id'].'">'.$row_grade['grade_id'].'</option>';
			}else{
				$opsi_pilihan .='<option value="'.$row_grade['grade_id'].'">'.$row_grade['grade_id'].'</option>';			
			}
			//var_dump($opsi_pilihan);
			$dropdown_extends = dropdown_multi_rows_extends_dua( $golongan_jabatan_id , $grade_id , "combobox_grade.php", $opsi_pilihan);
		}
	}else{	
		$dropdown_extends = dropdown_multi_rows_extends_dua( $golongan_jabatan_id , $grade_id , "combobox_grade.php", false );
	}
	$view .= form_field_display( $dropdown_extends  , "Grade"  );  	
	
	//END TAMBAHAN GRADE DAN GOL /****************/
	
	$gapok = array(
				'name'=>'gapok',
				'value'=>( isset($_POST['gapok']) ? $_POST['gapok'] : $kar['gapok_km'] ),
				'id'=>'gapok',
				'type'=>'text',
				'size'=>'45'
			);
	$form_field_nama = form_dynamic($gapok); 
	$view .= form_field_display( $form_field_nama   , "Gaji pokok"   );
	
	$catatan = array(
				'name'=>'catatan',
				'value'=>(isset($_POST['catatan']) ? $_POST['catatan'] : $fields['catatan'] ),
				'id'=>'catatan' ,'cols'=>'40','rows'=>'3'
			);
	$form_field_alamat = form_textarea($catatan);
	 
	$view .= form_field_display( $form_field_alamat , "Catatan khusus"   );	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	$reset = array(
		'value' => 'Reset',
		'name' => '', 
		'type'=>'reset','class'=>'main_button'
	);
	$form_reset= form_dynamic($reset);
	
	$view .= form_field_display( $form_submit .' '.$form_reset , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}


function report_karyawan(){
	$oopps = get_opsi_seting('karyawan_only');	
	my_set_code_js('
		function confirmDelete(i){
		var check = confirm(\'Akan mendelete data? \'); 
		if(check){
			location.href=\'index.php?com=karyawan&task=delete&id=\'+i;
		}
		return;	}
	');

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'nama'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		//'usia'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
		'jabatan'=>array('style'=>'border-bottom:2px solid;width:25%'), 
	//	'departemen'=>array('style'=>'border-bottom:2px solid;width:15%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:15%'), 
	);
	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM karyawan a 
			INNER JOIN biodata b ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian)  
			AND ( b.nama LIKE '%{$_GET['key']}%' OR a.nik ='{$_GET['key']}' )
			
		";
	}else{
		$query = "
				SELECT * FROM karyawan a 
				INNER JOIN biodata b ON a.biodata_id = b.biodata_id
				WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 
				ORDER BY a.nik		
		";
		
	}if(isset($_GET['th']))echo $query;	
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =15;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 15; 
	while($ey = my_fetch_array($result )){
		$i++;
		
		/* EDIT */
		$editproperty_admin = array(
						'href'=>'index.php?com=karyawan_installer&task=create&id='.$ey['karyawan_id'], 
						'title'=>'Edit'
				);
		$edit_button_admin = button_icon( 'b_edit.png' , $editproperty_admin  );
		$editproperty_standar = array(
						'href'=>'javascript:;',
						'onclick'=>'alert(\'Akses di tolak\')',
						'title'=>'Edit'
				);
		$edit_button_standar = button_icon( 'b_edit.png' , $editproperty_standar  );
		$edit_button = admin_link( $edit_button_admin , $edit_button_standar );
		/* END EDIT*/
		/* EDIT */
		$editprofil_admin = array(
						'href'=>'index.php?com=profil_kepegawaian&task=edit&id='.$ey['karyawan_id'], 
						'title'=>'Edit profil kepegawaian'
				);
		$editprofil_button_admin = button_icon( 'b_usredit.png' , $editprofil_admin  );
		$editprofil_standar = array(
						'href'=>'javascript:;',
						'onclick'=>'alert(\'Akses di tolak\')',
						'title'=>'Edit profil kepegawaian'
				);
		$editprofil_button_standar = button_icon( 'b_usredit.png' , $editprofil_standar  );
		$editprofil_button = admin_link( $editprofil_button_admin , $editprofil_button_standar );
		/* END EDIT*/
		
		/* DELETE */
		$deleteproperty_admin = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['karyawan_id'].');',
						'title'=>'Delete'
				);
		$delete_button_admin= button_icon( 'cut_on.gif' , $deleteproperty_admin  );
		$deleteproperty_standar = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:alert(\'Akses ditolak\')',
						'title'=>'Delete'
				);
		$delete_button_standar= button_icon( 'cut_on.gif' , $deleteproperty_standar  );
		$delete_button = admin_link( $delete_button_admin , $delete_button_standar );
		/* END DELETE */
		
		
		$detailproperty = array(
						'href'=>'index.php?com=karyawan&task=detail&page='.$current_page.'&id='.$ey['karyawan_id'],
						'title'=>'Detail'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		
		$skoringproperty = array(
						'href'=>'index.php?com=karyawan&task=skoring&id='.$ey['karyawan_id'],
						'rel'=>'facebox',
						'title'=>'Hasil seleksi'
				);
		$skoring_button = button_icon( 'paste_on.gif' , $skoringproperty  );
		
		$kekaryawanan = my_query("
		SELECT * FROM jabatan a 
		INNER JOIN kekaryawanan c ON a.jabatan_id=c.jabatan_id
		WHERE c.karyawan_id ={$ey['karyawan_id']} 
		LIMIT 1
		");
		$jbt_karya = my_fetch_array($kekaryawanan ); 
		$jab = my_get_data_by_id('jabatan','jabatan_id',(int) $jbt_karya['jabatan_id'] );  
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>position_text_align( $ey['nik'], 'center'),
			'nama'=>$ey['nama'],  
			//'usia'=>position_text_align(  date('Y') - date('Y' , strtotime($ey['tanggal_lahir'] ." 00:00:01") ) , 'center'), 
			'jabatan'=> $jbt_karya['nama_jabatan'],  
			//'divisi'=> $dep['departemen_name'] ,  
			'operasi'=> position_text_align($edit_button/*.' '.$delete_button */, 'right'),  
		); 
	}
	
	$datas = table_rows($row);
	
	 
	$menuontop = array(  
		'Tambah data'=>array('onclick'=>'javascript:location.href=\'index.php?com=karyawan_installer&task=create\''),
		//'Export to excel'=>array('onclick'=>'javascript:location.href=\'index.php?com=csv&task=excel_daftar_karyawan\''),
		//'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=all_karyawan\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	);
 
	
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" /><span style="font:10px verdana"><b>Pencarian cepat</b></span><br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" /><input class="simple_search" type="submit" value=" CARI " /> </form>';
	$printmenu = additional_menu_on_list( $menuontop , $search);
	$paging = $kgPagerOBJ ->showPaging();
	return $printmenu . table_builder($header , $datas ,  15 ,false , $paging  );
} 